import 'package:dk_flutter_base/dk_core.dart';
import 'package:flutter/widgets.dart';

typedef Widget WidgetBuilderByData(dynamic data);
typedef HandleErrorCode(DefaultResponseData responseData);
typedef OnRequestFinish();

typedef SwitchSuccessPage(dynamic data);
typedef SwitchErrorPage(Widget errorWidget);

///简化pageWidget网络请求 成功 回调的函数
///
///* switchPage:用以切换page视图，需要外部实现
///* showMsg:是否显示toast
///* handleErrorCode:错误业务码处理函数
Function handleNetSuccess4Page({
  SwitchSuccessPage switchPage,
  bool showMsg,
  HandleErrorCode handleErrorCode,
}) {
  showMsg ??= false;
  return (DefaultResponseData responseData, bool isBusinessSuccess) {
    dynamic data = responseData.data;
    String msg = responseData.msg;
    if (null != isBusinessSuccess && isBusinessSuccess) {
      if (showMsg && null != msg) {
        showShortToast(msg);
      }
      if (null != switchPage) {
        switchPage(data);
      }
      return;
    }
    if (null != handleErrorCode) {
      handleErrorCode(responseData);
    }
  };
}

///简化pageWidget，第二次（即可显示LoadingDialog）网络请求 成功 回调的函数
///
///* handleResult:网络成功且业务成功后处理业务数据的回调函数
///* showMsg:是否显示toast
///* handleErrorCode:错误业务码处理函数
///* onRequestFinish:网络请求结束后的回调函数，通常只是将LoadingDialog关闭
Function handleNetSuccess4Data({
  HandleResult handleResult,
  bool showMsg,
  HandleErrorCode handleErrorCode,
  OnRequestFinish onRequestFinish,
}) {
  showMsg ??= false;
  return (DefaultResponseData responseData, bool isBusinessSuccess) {
    if (null != onRequestFinish) {
      onRequestFinish();
    }
    dynamic data = responseData.data;
    String msg = responseData.msg;
    if (null != isBusinessSuccess && isBusinessSuccess) {
      if (showMsg && null != msg) {
        showShortToast(msg);
      }
      if (null != handleResult) {
        handleResult(data);
      }
      return;
    }
    if (null != handleErrorCode) {
      handleErrorCode(responseData);
    }
  };
}

///简化pageWidget网络请求 失败 回调的函数
///
///* showMsg:是否显示toast
///* withAppBarInDebug:是否在错误界面显示appBar以提供退出界面的方式
///* errorParam:错误界面显示logicPage样式参数
///* retryListener:错误界面显示logicPage中的点击重试
///* switchPage:用以切换page视图，需要外部实现
Function handleNetFail4Page({
  bool showMsg,
  bool withAppBarInDebug,
  LogicParam errorParam,
  LogicBtnClickListener retryListener,
  SwitchErrorPage switchPage,
}) {
  showMsg ??= false;
  withAppBarInDebug ??= false;
  Function result;
  switch (Env?.value?.type) {
    case EnvType.DEBUG:
    case EnvType.COMMIT_TEST:
    case EnvType.PRE_RELEASE:
      result = (DefaultHttpError error, dynamic originalError) {
        if (showMsg) {
          showShortToast(error?.errorMsg ?? "");
        }
        if (null != switchPage) {
          switchPage(HttpErrorWidget(
            error,
            originalError,
            withAppBarInDebug,
            onRetryClickListener: retryListener,
          ));
        }
      };
      break;
    case EnvType.RELEASE:
    default:
      result = (DefaultHttpError error) {
        if (showMsg) {
          showShortToast(error?.errorMsg ?? "");
        }
        if (null != switchPage) {
          switchPage(LogicPage.errorPage(
              errorParam: errorParam
                ..tip = error?.errorMsg
                ..listener = retryListener));
        }
      };
  }
  return result;
}

///简化pageWidget，第二次（即可显示LoadingDialog）网络请求 失败 回调的函数，和 BaseWidget网络请求 失败 回调函数
///
///* showMsg:是否显示toast
///* handleResult:网络错误时对错误信息的处理
///* onRequestFinish:网络请求结束后的处理
Function handleNetFail4Data({
  HandleResult2 handleResult,
  bool showMsg,
  OnRequestFinish onRequestFinish,
}) {
  return (DefaultHttpError error, dynamic originalError) {
    showMsg ??= false;
    if (null != onRequestFinish) {
      onRequestFinish();
    }
    if (showMsg) {
      showShortToast(error?.errorMsg ?? "");
    }
    if (null != handleResult) {
      handleResult(error, originalError);
    }
  };
}

///简化[handleNetFail4Data.handleResult]方法，仅针对
///根据当前app环境进行不同的处理，目前只处理非Release环境下的错误
///
///* retryListener:错误界面显示logicPage中的点击重试
///* switchPage:用以切换page视图，需要外部实现
HandleResult2 handleFailData({
  LogicBtnClickListener retryListener,
  SwitchErrorPage switchPage,
}) {
  return (dynamic error, dynamic originalError) {
    switch (Env?.value?.type) {
      case EnvType.DEBUG:
      case EnvType.COMMIT_TEST:
      case EnvType.PRE_RELEASE:
        if (null != switchPage) {
          switchPage(HttpErrorWidget(
            error,
            originalError,
            true,
            onRetryClickListener: retryListener,
          ));
        }
        break;
      case EnvType.RELEASE:
      default:
    }
  };
}

typedef HandleResult4BaseWidget(
    WidgetBloc widgetBloc, DefaultHttpError error, dynamic originalError);

///简化BaseWidget网络请求失败方法
///
///* widgetBloc:BaseWidget使用的bloc
///* handleResult:错误信息的处理
///* showMsg:是否显示msg
///* onRequestFinish:网络请求结束后的处理
Function handleNetFail4Widget({
  @required WidgetBloc widgetBloc,
  HandleResult4BaseWidget handleResult,
  bool showMsg,
  OnRequestFinish onRequestFinish,
}) {
  return (DefaultHttpError error, dynamic originalError) {
    showMsg ??= false;
    if (null != onRequestFinish) {
      onRequestFinish();
    }
    if (showMsg) {
      showShortToast(error?.errorMsg ?? "");
    }
    if (null != handleResult) {
      handleResult(widgetBloc, error, originalError);
    }
  };
}
